<?php
namespace App\Http\Controllers\Admin;


use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Response;
use App\Models\Attachment;
use App\Models\TeachesContentVideo;
use App\Models\TeachesCompanyVideo;
use Illuminate\Support\Facades\Auth;
use App\Models\TeachesContentOffice;
use App\Models\TeachesCompanyOffice;
use App\Models\TeachesContentPpt;
use App\Models\TeachesCompanyPpt;
use Intervention\Image\Facades\Image;
class UploadController extends BaseController
{
    
    /**
     * 图片上传   百度经验方式的文章
     */
    public function newsImages()
    {
        $file = Request::file('upfile');
        $base_dir = public_path();
        $type = Request::input('type');
        if(!empty($type))
        {
            $app = $type;
        }
        else 
        {
            $app = 'news';
        }
        $filePath = "/uploads/images/".$app."/".date("Ymd")."/";
        if($file->isValid()){
            $clientName = $file->getClientOriginalName();
            
            
            $extension = $file->getClientOriginalExtension();
            
            
            $newName = md5(date('ymdhis').$clientName).".".$extension;
            
            $file->move($base_dir.$filePath,$newName);
            
            return Response::json(array('url'=>$filePath.$newName,'name'=>$newName,'status'=>'success','msg'=>'上传图片成功！'));
        }
        else
        {
            return Response::json(array('url'=>'','status'=>'false','msg'=>'上传图片失败！'));
        }
    }
    
    /**
     * 图片上传
     */
    public function employImg(){
        $file = Request::file('upfile');
        $type = Request::input('type');
        if(!empty($type))
        {
            $app = $type;
        }
        else
        {
            $app = 'news';
        }
        $base_dir = public_path();
        $filePath = "/uploads/images/".$app."/".date("Ymd")."/";
        if($file->isValid()){
            $clientName = $file->getClientOriginalName();
        
            $extension = $file->getClientOriginalExtension();
        
            $mimeTye = $file->getMimeType();
        
            $newName = md5(date('ymdhis').$clientName).".".$extension;
        
            $file->move($base_dir.$filePath,$newName);
            
            $attach = new Attachment();
            
            $attach->app = $app;
            $attach->att_name = $newName;
            $attach->url = $filePath.$newName;
            $attach->type = $mimeTye;
            $attach->save();
        
            return Response::json(array('attach'=>$attach->url,'attach_id'=>$attach->aid,'status'=>'success','msg'=>'上传图片成功！'));
        }
        else
        {
            return Response::json(array('attach'=>'','status'=>'false','msg'=>'上传图片失败！'));
        }
    }

    /**
     * 图片上传
     */
    public function uploadImg(){
        $app = Request::input('app');
        $file = Request::file('upfile');
        $base_dir = public_path();
        $filePath = "/uploads/images/" . $app . "/".date("Ymd")."/";
        if($file->isValid()){
            $clientName = $file->getClientOriginalName();

            $extension = $file->getClientOriginalExtension();
            $mimeTye = $file->getMimeType();

            $newName = md5(date('ymdhis').$clientName).".".$extension;

            $file->move($base_dir.$filePath,$newName);

            $attach = new Attachment();

            $attach->app = $app;
            $attach->att_name = $newName;
            $attach->url = $filePath.$newName;
            $attach->type = $mimeTye;
            $attach->save();

            return Response::json(array('attach'=>$attach->url,'attach_id'=>$attach->aid,'status'=>'success','msg'=>'上传图片成功！'));
        }
        else
        {
            return Response::json(array('url'=>'','status'=>'false','msg'=>'上传图片失败！'));
        }
    }
    
    /**
     * 视频上传 
     */
    public function video()
    {
        $file = Request::file('upfile');
        $app = Request::input('app');
        $filePath = "/uploads/video/teaches/".date("Ymd")."/";
        if($file->isValid()){
            $clientName = $file->getClientOriginalName();
        
            $extension = $file->getClientOriginalExtension();
        
            $mimeTye = $file->getMimeType();
        
            $newName = md5(date('ymdhis').$clientName);
            $newfileName = $newName.".".$extension;
                    
            $file->move(public_path($filePath),$newfileName);
            if($app == 'teaches_content')
            {
                $video = new TeachesContentVideo();
            }
            elseif($app == 'teaches_company')
            {
                $video = new TeachesCompanyVideo();
            }
        
            $video->url = $filePath.$newfileName;
            $video->type = $mimeTye;
            $video->size = $file->getClientSize();
            $video->user_id = Auth::user()->id;
            $video->save();
        
            return Response::json(array('url'=>$video->url,'id'=>$video->id,'status'=>'success','msg'=>'上传视频成功！'));
        }
        else
        {
            return Response::json(array('url'=>'','status'=>'false','msg'=>'视频失败！'));
        }
    }
    
    /**
     * 文档上传
     */
    public function office()
    {
        $file = Request::file('upfile');
        $app = Request::input('app');
        $filePath = "/uploads/office/teaches/".date("Ymd")."/";
        if($file->isValid()){
            $clientName = $file->getClientOriginalName();
    
            $extension = $file->getClientOriginalExtension();
    
            $mimeTye = $file->getMimeType();
    
            $newName = md5(date('ymdhis').$clientName);
            
            $newfileName = $newName.".".$extension;
    
            $file->move(public_path($filePath),$newfileName);
            if($app == 'teaches_content')
            {
                $video = new TeachesContentOffice();
            }
            elseif($app == 'teaches_company')
            {
                $video = new TeachesCompanyOffice();
            }
            $swf = office2swf($filePath.$newfileName,$newName,$filePath);
            if(empty($swf))
            {
                return Response::json(array('status'=>'false','msg'=>'转化格式失败！'));
            }
            elseif(empty($swf['pdfurl']) || empty($swf['swfurl']))
            {
                return Response::json(array('status'=>'false','msg'=>'转化格式失败！','url'=>''));
            }
            $video->url = $filePath.$newfileName;
            $video->file_name = $newName;
            $video->size = $file->getClientSize();
            $video->file_path = $filePath;
            $video->type = $mimeTye;
            $video->user_id = Auth::user()->id;
            $video->pdf_url = $swf['pdfurl'];
            $video->swf_url = $swf['swfurl'];
            $video->save();
    
            return Response::json(array('url'=>$video->url,'id'=>$video->id,'status'=>'success','msg'=>'上传教程成功！'));
        }
        else
        {
            return Response::json(array('url'=>'','status'=>'false','msg'=>'上传失败！'));
        }
    }
    
    /**
     * PPT 教程上传  zip格式文件
     * 需要解压缩
     */
    public function ppt()
    {
        $file = Request::file('upfile');
        $app = Request::input('app');
        if($app == 'teaches_content')
        {
            $video = new TeachesContentPpt();
            $filePath = "/uploads/ppt/teaches/".date("Ymd")."/";
        }
        elseif($app == 'teaches_company')
        {
            $video = new TeachesCompanyPpt();
            $filePath = "/uploads/ppt/company/".date("Ymd")."/";
        }
        else 
        {
            abort(404);
        }
        
        if($file->isValid()){
            $clientName = $file->getClientOriginalName();
    
            $extension = $file->getClientOriginalExtension();
    
            $mimeTye = $file->getMimeType();
    
            $newName = md5(date('ymdhis').$clientName);
    
            $newfileName = $newName.".".$extension;
    
            $file->move(public_path($filePath),$newfileName);
            $zip = new \ZipArchive();
            if($zip->open(public_path($filePath.$newfileName)) === TRUE)
            {
                $index=$zip->locateName('index.html', \ZipArchive::FL_NOCASE);
                $contents = $zip->getFromIndex($index);
                if(empty($contents))
                {
                    unlink(public_path($filePath.$newfileName));
                    return Response::json(array('status'=>'false','msg'=>'压缩包请必须包含index.html！'));
                }
                
                $zip->extractTo(public_path($filePath.$newName));//假设解压缩到在当前路径下images文件夹的子文件夹php
                $zip->close();//关闭处理的zip文件
                unlink(public_path($filePath.$newfileName));
                $video->url = $filePath.$newName."/index.html";
                $video->file_name = $newName;
                $video->size = $file->getClientSize();
                $video->file_path = $filePath;
                $video->type = $mimeTye;
                $video->user_id = Auth::user()->id;
                $video->save();
                return Response::json(array('url'=>$video->url,'id'=>$video->id,'status'=>'success','msg'=>'上传教程成功！'));
            }
            else 
            {
                return Response::json(array('url'=>'','status'=>'false','msg'=>'上传失败！'));
            }
    
        }
        else
        {
            return Response::json(array('url'=>'','status'=>'false','msg'=>'上传失败！'));
        }
    }
    
    /**
     * 用户上传头像 
     */
    public function userLogo($id)
    {
        $file = Request::file('upfile');
        $userPath = md5($id);
        $logoPath = substr($userPath,0,5).'/'.substr($userPath, 5,10).'/'.substr($userPath, 10).'/';
        $logo = '/uploads/avatar/'.$logoPath;
        if($file->isValid()){
            $extension = $file->getClientOriginalExtension();
//             $mimeTye = $file->getMimeType();
            $newfileName = 'original.'.$extension;
            $bigName = 'big.'.$extension;
            $middleName = 'middle.'.$extension;
            $smallName = 'small.'.$extension;
            $file->move(public_path($logo),$newfileName);
            $originalPath = public_path($logo.$newfileName);
            $bigPath = public_path($logo.$bigName);
            $middlePath = public_path($logo.$middleName);
            $smallPath = public_path($logo.$smallName);
            
            $img = Image::make($originalPath);
            $img->resize(120,120);
            $img->save($middlePath);
            $img->resize(200,200);
            $img->save($bigPath);
            $img->resize(80,80);
            $img->save($smallPath);
            
            return Response::json(array('attach'=>$logo."small.".$extension,'status'=>'success','msg'=>'上传用户头像成功！'));
            
        }
    }
}

